package untiring07.mvc.dao;

import java.util.List;

import mybatis3.helper.MVCSqlSessionFactory;

import org.apache.ibatis.session.SqlSession;

import untiring07.mvc.model.BoardVO;
import untiring07.mvc.model.ParamVO;

public class BoardDAO {
	
	private static int perpage = 20;
	public static List<BoardVO> listBoard(int startno, String ftype, String fkey){
		List<BoardVO> result = null;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setStartno(startno);
		pvo.setPerpage(perpage);
		pvo.setFtype(ftype);
		pvo.setFkey(fkey);
		try{
			result = sess.selectList("BoardMapper.findlistSQL",pvo);
		}finally{
			sess.close();
		}return result;
	}
	
	public static int totalBoard(String ftype,String fkey){
		int result = -1;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setPerpage(perpage);
		pvo.setFtype(ftype);
		pvo.setFkey(fkey);
		try{
			result = sess.selectOne("BoardMapper.totalSQL",pvo);
		}finally{
			sess.close();
		}return result;
	}
	/*public static BoardVO viewBoard(BoardVO b) {
		SqlSession sess = MVCSqlSessionFactory.openSession();
		BoardVO result = new BoardVO();
		try {
			
			result = sess.selectOne("BoardMapper.viewSQL",b);
		
		} finally {
			sess.close();
		}
		return result;
	}*/
	public static boolean writeBoard(BoardVO b) {
		boolean result = false;
		SqlSession sess = null;
		try{
			sess = MVCSqlSessionFactory.openSession();
			if(sess.insert("BoardMapper.insertSQL",b)>0){
				result = true;
			}
			sess.commit(); //insert나 update시 반드시 작성!!
		}finally{
			sess.close();
		}
		return result;
		
	}
	public static BoardVO viewBoard(String bdno){
		BoardVO result = null;
		SqlSession sess = null;
		try{
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("BoardMapper.viewSQL",bdno);
		}finally{
			sess.close();
		}
		return result;
	}
	

	public static void viewCountBoard(String bdno){
		SqlSession sess = null;
		try{
			sess = MVCSqlSessionFactory.openSession();
			sess.update("BoardMapper.viewcountSQL",bdno);
			sess.commit();
		}finally{
			sess.close();
		}
		
		
		
	}
	
	
	
	public static boolean updateBoard(String bdno){
		boolean result = false;
		SqlSession sess = null;
		try{
			sess = MVCSqlSessionFactory.openSession();
			if(sess.update("BoardMapper.insertSQL",bdno)>0){
				result = true;
			}
			sess.commit(); //insert나 update시 반드시 작성!!
		}finally{
			sess.close();
		}
		return result;
	}
}
